﻿using System;
using System.Collections.Generic;
using SqlParameter = System.Data.SqlClient.SqlParameter;
using SqlDbType = System.Data.SqlDbType;
using IDataReader = System.Data.IDataReader;

namespace GamePlusClassLibrary
{
    public class Tags
    {
        public int? Idf_Tag { get; set; }
        public string Des_Tag { get; set; }

        public static Tags Carregar(int pIdTag)
        {
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(new SqlParameter("@Idf_Tag", SqlDbType.Int));
            param[0].Value = pIdTag;

            string sQuery = @"SELECT * FROM Tags WHERE Idf_Tag = @Idf_Tag";

            Tags obj = null;

            using (IDataReader iDr = DataAccessLayer.ExecuteReader(System.Data.CommandType.Text, sQuery, param))
            {
                if (iDr.Read())
                {
                    obj = new Tags();
                    obj.Idf_Tag = int.Parse(iDr["Idf_Tag"].ToString());
                    obj.Des_Tag = iDr["Des_Tag"].ToString();
                }
            }

            return obj;
        }

        public static List<Tags> Listar(int pTop, string pTexto)
        {
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(new SqlParameter("@Top", SqlDbType.Int));
            param.Add(new SqlParameter("@Texto", SqlDbType.VarChar));
            param[0].Value = pTop;
            param[1].Value = pTexto;

            string sQuery =
            @"
                SET ROWCOUNT @Top
                SELECT * FROM Tags WHERE Des_Tag LIKE '' + @Texto + '%' ORDER BY Des_Tag 
            ";

            List<Tags> ls = new List<Tags>();

            using (IDataReader iDr = DataAccessLayer.ExecuteReader(System.Data.CommandType.Text, sQuery, param))
            {
                while (iDr.Read())
                {
                    Tags obj = new Tags();
                    obj.Idf_Tag = int.Parse(iDr["Idf_Tag"].ToString());
                    obj.Des_Tag = iDr["Des_Tag"].ToString();
                    ls.Add(obj);
                }
            }

            return ls;
        }
    }
}
